import app from "@/app";
import { ProjBasicInfo, emptyBasicInfo, Data } from "./ProjBasicInfo";
import { Member, MemberViewModel } from "./Member";
import { Budget, BudgetViewModel } from "./Budget";
import {emptyExtInfo, ExtInfo} from "@proj/types/ExtInfo.ts";

export class Project {
    basicInfo: ProjBasicInfo = emptyBasicInfo
    budgets: Budget[] = []
    members: Member[] = []
    extInfo: ExtInfo = emptyExtInfo
    otherTeachers: Member[] = []
    canEdit: boolean = false
    canSee: boolean = false
    accessLevel: number = 0
    failMsg: string = ""

    constructor () {

    }

    static fromGetProjResponse (getProjResponse : any) : Project {
        const proj = new Project();
        proj.basicInfo = getProjResponse.basicInfo;
        proj.budgets = getProjResponse.budgets;
        proj.extInfo = getProjResponse.extInfo;
        proj.otherTeachers = getProjResponse.otherTeachers;
        proj.canSee = getProjResponse.success;
        proj.canEdit = getProjResponse.canEdit;
        proj.failMsg = getProjResponse.msg;
        proj.accessLevel = getProjResponse.accessLevel;
        proj.members = getProjResponse.members;
        proj.setPermission();
        console.log(proj);

        return proj;
    }

    setPermission () : void {
        this.basicInfo.canSee = this.canSee;
        this.basicInfo.canEdit = this.canEdit;
        this.basicInfo.readonly = !this.canEdit;
        this.basicInfo.isNew = !this.basicInfo.ProjectName;
        this.basicInfo.accessLevel = this.accessLevel;
    }

    toTeachersMemberViewModel () : MemberViewModel[] {
        let vm : MemberViewModel[] = []

        if (this.basicInfo.GuideTeacherId !== app.GUID_EMPTY) {
            vm.push({
                memberOrder: 1,
                code: this.basicInfo.GuideTeacherCode,
                realName: this.basicInfo.GuideTeacher,
                major: "",
                reformType: "",
                department: "",
                division: "",
                score: 0,
                relateId: "",
            })
        } else {
            vm.push({
                memberOrder: 1,
                code: "",
                realName: "",
                major: "",
                reformType: "",
                department: "",
                division: "",
                score: 0,
                relateId: "",
            })
        }

        vm = [ ...vm, ...this.otherTeachers.map(e => {
            let member : MemberViewModel = {
                memberOrder: 1,
                code: e.Code,
                realName: e.RealName,
                major: "",
                reformType: "",
                department: "",
                division: "",
                score: 0,
                relateId: "",
            };
            return member;
        })];

        for (let i = 0; i < vm.length; i++) {
            vm[i].memberOrder = (i + 1);
        }

        return vm;
    }

    toStudentsMemberViewModel () {
        let vm : MemberViewModel[] = []

        if (this.basicInfo.ProjOwnerId !== app.GUID_EMPTY) {
            vm.push({
                memberOrder: 1,
                code: this.basicInfo.ProjOwnerCode,
                realName: this.basicInfo.ProjOwner,
                major: this.basicInfo.ProjOwnerMajor,
                reformType: this.basicInfo.ProjOwnerReformType,
                department: this.basicInfo.ProjOwnerDepartment,
                division: "负责人",
                score: 0,
                relateId: "",
            })
        }

        vm = [ ...vm, ...this.members.map(e => {
            let member : MemberViewModel = {
                memberOrder: 1,
                code: e.Code,
                realName: e.RealName,
                major: e.Major,
                reformType: e.ReformType,
                department: e.Department,
                division: e.Division,
                score: e.Score,
                relateId: e.RelateId,
            };
            return member;
        })];

        for (let i = 0; i < vm.length; i++) {
            vm[i].memberOrder = (i + 1);
        }

        return vm;
    }

    setMembers (basicInfo : ProjBasicInfo, otherTeachers : Member[], members : Member[]) {
        this.basicInfo = basicInfo;
        this.otherTeachers = otherTeachers;
        this.members = members;
    }

    toBudgetViewModel () : BudgetViewModel[] {
        return BudgetViewModel.fromModel(this.budgets);
    }

    setBudgets (budgets : Budget[]) {
        this.budgets = budgets;
    }

    static getEmptyProject () : Project {
        return new Project();
    }
}

export default Project;